package com.example.services;

import android.util.Log;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by DELL on 2016/8/23.
 */
public class DynamicTimerTask extends TimerTask {
    private int counter;

    public void setPeriod(int interval) {
        setDeclaredField(TimerTask.class, this, "period", interval);
    }

    static boolean setDeclaredField(Class<?> clazz, Object obj, String name, Object value) {
        try {
            Field field = clazz.getDeclaredField(name);
            field.setAccessible(true);
            field.set(obj, value);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    public void run() {
        Log.d(getClass().toString(), String.valueOf(++counter));
    }
}
